package ru.dc.gui.ncurses;

import charva.awt.*;
import charva.awt.event.ActionEvent;
import charva.awt.event.ActionListener;
import charvax.swing.*;


/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 11.10.2010
 * Time: 14:27:06
 * </pre>
 */
public class ConnectionParamsDlg extends JDialog {
    private JTextField addr;
    private JTextField nick;
    private int result = JOptionPane.CANCEL_OPTION;

    public ConnectionParamsDlg(Frame owner) {

        super(owner);
        setModal(true);
        setSize(30, 5);

        setTitle("Enter parameters");

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel addrPanel = new JPanel(new BorderLayout());
        final JLabel addrLabel = new JLabel("Address:");
        addrLabel.setLength(10);
        addrPanel.add(addrLabel, BorderLayout.WEST);
        addr = new JTextField();
        addr.setColumns(30);
        addrPanel.add(addr, BorderLayout.CENTER);
        add(addrPanel);

        JPanel nickPanel = new JPanel(new BorderLayout());
        final JLabel nickLabel = new JLabel("Nick:");
        nickLabel.setLength(10);
        nickPanel.add(nickLabel, BorderLayout.WEST);
        nick = new JTextField();
        nick.setColumns(30);
        nickPanel.add(nick, BorderLayout.CENTER);
        add(nickPanel);


        JPanel btnsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 1, 0));
        JButton okBtn = new JButton("OK");
        okBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                onOk();
            }
        });
        btnsPanel.add(okBtn);
        JButton cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                onCancel();
            }
        });
        btnsPanel.add(cancelBtn);
        add(btnsPanel);

        setLocation((Application.getApplication().getMainFrame().getWidth()-getWidth())/2,
                (Application.getApplication().getMainFrame().getHeight()-getHeight())/2);
        validate();

    }

    private void onCancel() {
        result = JOptionPane.CANCEL_OPTION;
        hide();
    }

    private void onOk() {
        result = JOptionPane.OK_OPTION;
        hide();
    }


    public int execute() {
        show();
        return result;
    }

    public String getAddress() {
        return addr.getText();
    }

    public String getNick() {
        return nick.getText();
    }
}
